<template>
  <!-- 项目评估 -->
  <div class="app-container">
    <div class="mainbox">
      <!-- 顶部操作 -->
      <div class="filter-container">
        <el-form ref="filterFormRef" :inline="true" :model="filterForm">
          <el-form-item label="文件名称:" prop="fileOriginName">
            <el-input
              v-model.trim="filterForm.fileOriginName"
              class="filter-item limit"
              style="width: 200px;margin-right:10px"
              maxlength="30"
              clearable
              placeholder="请输入文件名称"
              @keyup.enter.native="$_handleFilter"
            />
          </el-form-item>
          <el-form-item label="文件存储位置:" prop="fileLocation">
            <el-select v-model.trim="filterForm.fileLocation" placeholder="请选择" :clearable="true">
              <el-option
                v-for="item in fileLocationList"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
        </el-form>
        <div class="flex-1"></div>
        <el-button type="primary" @click="$_handleFilter">查询</el-button>
        <el-button @click="$_handleReset">重置</el-button>
      </div>
      <!-- 表格 -->
      <div class="table-container table-fullscreen">
        <div class="table-opt-container">
          <el-button
            v-permission="['SPECIALCLASSES_ADD_BUTTON']"
            type="primary"
            icon="el-icon-upload2"
            @click="fileUploadVisible = true"
          >
            添加上传
          </el-button>
          <div class="flex-1" />
          <dt-dialog-column :columns="columns" @queryTable="getList" />
        </div>
        <el-table
          ref="table"
          v-loading="MX_listLoading"
          :data="MX_list"
          border
          highlight-current-row
          :row-key="$_getRowKey"
          style="width: 100%;text-align:center"
          :header-cell-style="MX_headerCellStyle"
        >
          <template slot="empty">
            <p>{{ $store.getters.dataText }}</p>
          </template>
          <el-table-column
            fixed="left"
            type="index"
            label="序号"
            width="70"
            align="center"
            :index="$_tableIndex"
          />
          <el-table-column
            align="center"
            min-width="130"
            label="文件/图片"
          >
            <template slot-scope="{row}">
              <el-image
                v-if="['png', 'jpg', 'jpeg'].includes(row.fileSuffix)"
                style="width:90px;height: 90px;"
                :preview-src-list="imageSrcList"
                :src="row.fileUrl ||baseURL() + '/sysFileInfo/previewByObjectName?fileBucket=' +
                  row.fileBucket + '&fileObjectName=' + row.fileObjectName"
                fit="contain"
              />
              <div v-else> {{ row.fileSuffix }} 文件</div>
            </template>
          </el-table-column>
          <el-table-column
            v-for="item in columns"
            :key="item.prop"
            :prop="item.prop"
            :label="item.label"
            :width="item.width"
            :min-width="item.minWidth"
            :formatter="item.formatter"
            align="center"
            show-overflow-tooltip
          />
          <el-table-column
            :key="Math.random()"
            fixed="right"
            label="操作"
            width="240px"
            align="center"
          >
            <template slot-scope="scope">
              <el-button
                size="mini"
                class="tBtn"
                @click="downloadImage(scope.row)"
              >下载</el-button>
              <el-button
                size="mini"
                class="tBtn"
                @click="shareImage(scope.row)"
              >分享</el-button>
              <el-button
                size="mini"
                class="tBtn"
                @click="handleDetail(scope.row)"
              >详情</el-button>
              <el-button
                v-permission="['RESOURCELIBRARY_DEL_BUTTON']"
                size="mini"
                type="danger"
                class="tBtn"
                @click="handleDelete(scope.row)"
              >删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页器 -->
        <dt-pagination
          v-show="MX_total > 0"
          :total="MX_total"
          :page.sync="MX_pageNav.pageNo"
          :limit.sync="MX_pageNav.pageSize"
          @pagination="getList"
        />
      </div>
      <!-- 文件上传 -->
      <dtDialog
        title="文件上传"
        :visible.sync="fileUploadVisible"
        :footer-slot="true"
        width="620px"
      >
        <el-form slot="content">
          <FormTemplate :form-item="{type:'file', prop: 'file', callBack: uploadFileSuccess}"></FormTemplate>
        </el-form>
      </dtDialog>
      <DialogForm ref="dialogForm" labelWidth="140px" :formDesignList="formDesignList">
        <div slot="append" v-if="!!currentDetail && ['png', 'jpg', 'jpeg'].includes(currentDetail.fileSuffix)">
          <el-form label-width="140px" label-position="left">
            <el-form-item label="图片">
              <el-image
                style="width:90px;height: 90px;"
                :preview-src-list="imageSrcList"
                :src="currentDetail.fileUrl || baseURL() + '/sysFileInfo/previewByObjectName?fileBucket=' +
                  currentDetail.fileBucket + '&fileObjectName=' + currentDetail.fileObjectName"
                fit="contain"
              />
            </el-form-item>
          </el-form>
        </div>
      </DialogForm>
    </div>
    <!-- 分享 -->
    <ResourceLibraryShare ref="shareDialg">
      <el-form-item label="图片" v-if="!!currentDetail">
        <el-image
          v-if="['png', 'jpg', 'jpeg'].includes(currentDetail.fileSuffix)"
          style="width:90px;height: 90px;"
          :src="currentDetail.fileUrl || baseURL() + '/sysFileInfo/previewByObjectName?fileBucket=' +
                  currentDetail.fileBucket + '&fileObjectName=' + currentDetail.fileObjectName"
          fit="contain"
        />
        <div v-else> {{ currentDetail.fileOriginName }}</div>
      </el-form-item>
    </ResourceLibraryShare>
  </div>
</template>
<script>
import DialogForm from '@/views/investment/components/DialogForm'
import commonList from "@/utils/mixins/investment/commonList";
import systemApi from "@/api/investment/system";
import FormTemplate from "@/views/investment/components/FormTemplate";
import ResourceLibraryShare from "./ResourceLibraryShare"
import {cloud} from "@/framework/utils/request";
import {getToken} from "@/framework/utils/auth";
const fileLocationList = [{value: 1,label:'阿里云'}, {value: 2,label:'腾讯云'}, {value: 3,label:'minio'}, {value: 4,label:'本地'}]
export default {
  name: 'ResourceLibrary',
  components: {
    DialogForm,
    FormTemplate,
    ResourceLibraryShare
  },
  mixins: [commonList],
  data() {
    return {
      filterForm: {
        fileOriginName: '',
        fileLocation: null
      },
      fileLocationList,
      // 列信息
      columns: [
        { prop: 'fileOriginName', label: `文件名称`, minWidth: '180px' },
        { prop: 'fileLocation', label: `存储位置`, width: '140px', formatter: function(row) {
            let tText = '';
            switch (row.fileLocation){
              case 1:
                tText = '阿里云'
                break;
              case 2:
                tText = '腾讯云'
                break;
              case 3:
                tText = 'minio'
                break;
              case 4:
                tText = '本地'
                break;
            }
            return tText
          } },
        { prop: 'fileSuffix', label: `文件后缀`, width: '90px' },
        { prop: 'fileSizeInfo', label: `文件大小`, width: '90px' },
        { prop: 'fileBucket', label: `文件仓库`, width: '120px' },
        { prop: 'fileBelongTo', label: `归属`, width: '120px' },
        { prop: 'createTime', label: `创建时间`, width: '220px' },
        { prop: 'fileBelongToUserName', label: `创建人`, width: '100px' }
      ],
      formDesignList: [
        { type: 'text', prop: 'fileOriginName', label: `文件名称` },
        { type: 'select', prop: 'fileLocation', label: `存储位置`, disabled: true, dictOptions: fileLocationList },
        { type: 'text', prop: 'fileBucket', label: `文件仓库` },
        { type: 'text', prop: 'fileCode', label: `文件编码` },
        { type: 'text', prop: 'fileId', label: `文件ID` },
        // { type: 'text', prop: 'secretFlag', label: `是否为机密文件` },
        { type: 'text', prop: 'fileSuffix', label: `文件后缀` },
        { type: 'text', prop: 'fileSizeInfo', label: `文件大小` },
        { type: 'textarea', prop: 'fileUrl', label: `存储路径` },
        { type: 'text', prop: 'createTime', label: `创建时间` },
        { type: 'text', prop: 'fileBelongToUserName', label: `创建人` }
      ],
      idKeyName: 'id',
      currentDetail: null,
      fileUploadVisible: false,
      getDetailApi: systemApi.detailFileItem
    }
  },
  computed: {
    imageSrcList(){
      let list = []
      this.MX_list.map((row) => {
        list.push(row.fileUrl || baseURL() + '/sysFileInfo/previewByObjectName?fileBucket=' +
          row.fileBucket + '&fileObjectName=' + row.fileObjectName)
      })
      return list
    }
  },
  created() {
    this.getList()
  },
  activated() {
    this.$_resetTableLayout()
  },
  methods: {
    baseURL() {
      return process.env.VUE_APP_BASE_API + cloud.file
    },
    // 获取页面列表
    getList() {
      this.$_getList(systemApi.getFiledList,{
        ...this.filterForm,
        ...this.MX_pageNav
      })
    },
    shareImage(data){
      this.currentDetail = data
      setTimeout(() => {
        this.$refs.shareDialg.open(data)
      })
    },
    handleDetail(data){
      this.currentDetail = data
      this.$_handleDetail(data, this.getDetailApi, this.idKeyName)
    },
    // 删除按钮
    handleDelete(data) {
      this.$dtMessageBox({ title: '提示', message: '确定要删除当前数据吗?', type: 'warning' }).then(() => {
        systemApi.delFileItem({
          fileCode: data.fileCode,
          fileId: data.id
        }).then((res) => {
          this.$dtMessage({
            type: 'success',
            message: '删除成功'
          })
          if (this.MX_listLength == 1) {
            this.MX_pageNav.pageNo--
          }
          this.getList()
        })
      })
    },
    downloadImage(row) {
      let url = this.baseURL()
      const hasToken = getToken()
      if (row.secretFlag === 'Y') {
        url += `/sysFileInfo/privateDownload?fileId=${row.fileId}&token=${hasToken}`
      } else {
        url += `/sysFileInfo/publicDownload?fileId=${row.fileId}&token=${hasToken}`
      }
      window.location.href = url
    },
    uploadFileSuccess(fileInfo){
      this.fileUploadVisible = false
      this.getList()
    }
  }
}
</script>

<style lang="scss" scoped>
.tBtn{
  margin-top: 3px;
  margin-bottom: 3px;
}
</style>
